package data;

import java.util.List;

import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import utils.SessionFactoryUtil;
import domain.Noticias;
import domain.NoticiasImpl;

public class JDBCNoticiasDao implements INoticiasDao {
	
	final static Logger logger = LoggerFactory.getLogger(JDBCNoticiasDao.class);
	
	public JDBCNoticiasDao(){
		
	}

	
	public boolean insertarNoticia(String titulo, String descripcion) {
		
		Session sesion = SessionFactoryUtil.getInstance().getCurrentSession();
		sesion.beginTransaction();
		
		
		try{
			Noticias noticia= new NoticiasImpl(titulo, descripcion);
			sesion.save(noticia);
		}catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		
		sesion.getTransaction().commit();
		return true;
		
	  }


	
	public List<Noticias> listarNoticias() {
		
		Session sesion = SessionFactoryUtil.getInstance().getCurrentSession();

		sesion.beginTransaction();

		List<Noticias> result = sesion.createQuery("FROM Noticias ").setMaxResults(20).list();
		
		sesion.getTransaction().commit();
		
		return result;
	}
	
	public void borrarNoticia(int idNoticia){
		Session sesion = SessionFactoryUtil.getInstance().getCurrentSession();
		sesion.beginTransaction();
		NoticiasImpl noticia= new NoticiasImpl();
		try{
		noticia.setIdNoticia(idNoticia);
		sesion.delete(noticia);
		}catch (Exception e) {
			e.printStackTrace();
			
		}
		
		sesion.getTransaction().commit();
		
		
		
	}
	
	
	
}		  